<template>
	<el-dialog :model-value="visible" title="订单信息" width="400px" @close="close" :close-on-click-modal="false">
		<el-form :model="form" label-width="90px" :rules="rules" ref="formRef">
			<el-form-item label="订单编号" prop="orderNo">
				<el-input v-model="form.orderNo" />
			</el-form-item>
			<el-form-item label="产品名称" prop="product">
				<el-input v-model="form.product" />
			</el-form-item>
			<el-form-item label="数量" prop="quantity">
				<el-input v-model="form.quantity" type="number" />
			</el-form-item>
			<el-form-item label="订单状态" prop="status">
				<el-select v-model="form.status" placeholder="请选择">
					<el-option label="待生产" value="待生产"></el-option>
					<el-option label="生产中" value="生产中"></el-option>
					<el-option label="已完成" value="已完成"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="客户" prop="customer">
				<el-input v-model="form.customer" />
			</el-form-item>
			<el-form-item label="下单日期" prop="orderDate">
				<el-date-picker v-model="form.orderDate" type="date" value-format="YYYY-MM-DD" placeholder="选择日期" />
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="close">取消</el-button>
			<el-button type="primary" @click="submit">保存</el-button>
		</template>
	</el-dialog>
</template>

<script setup>
import { ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
const props = defineProps({
	visible: Boolean,
	editData: Object
})
const emit = defineEmits(['update:visible', 'save'])

const form = ref({
	orderNo: '',
	product: '',
	quantity: 1,
	status: '待生产',
	customer: '',
	orderDate: ''
})

const rules = {
	orderNo: [{ required: true, message: '请输入订单编号', trigger: 'blur' }],
	product: [{ required: true, message: '请输入产品名称', trigger: 'blur' }],
	quantity: [{ required: true, message: '请输入数量', trigger: 'blur' }],
	status: [{ required: true, message: '请选择订单状态', trigger: 'change' }],
	customer: [{ required: true, message: '请输入客户', trigger: 'blur' }],
	orderDate: [{ required: true, message: '请选择下单日期', trigger: 'change' }]
}

const formRef = ref()

watch(() => props.editData, (val) => {
	if (val) {
		form.value = { ...val }
	} else {
		form.value = {
			orderNo: '',
			product: '',
			quantity: 1,
			status: '待生产',
			customer: '',
			orderDate: ''
		}
	}
}, { immediate: true })

function close() {
	emit('update:visible', false)
}

function submit() {
	formRef.value.validate(valid => {
		if (valid) {
			emit('save', { ...form.value })
		} else {
			ElMessage.warning('请完整填写表单')
		}
	})
}
</script>
  
